CSharpTest.Net
LockingFactory Property
See Also  Example Send Feedback Download Help File
CSharpTest.Net.BPlusTree Assembly > CSharpTest.Net.Collections Namespace > BPlusTreeOptions<TKey,TValue> Class : LockingFactory Property

Glossary Item Box

Gets or sets the locking factory to use for accessing shared data. The default is WriterOnlyLocking() which does not perform read locks, rather it will rely on the cache of the btree and may preform dirty reads. You can use any implementation of ILockFactory; however, the SimpleReadWriteLocking seems to perform the most efficiently for both reader/writer locks. Additionally wrapping that instance in a ReserveredWriterLocking() instance will allow reads to continue up until a writer begins the commit process. If you are only accessing the BTree instance from a single thread this can be set to IgnoreLocking. Be careful of using ReaderWriterLocking as the write-intesive nature of the BTree will suffer extreme performance penalties with this lock.

Syntax

Visual Basic (Declaration) 
Public Property LockingFactory As ILockFactory
C# 
public ILockFactory LockingFactory {get; set;}

Example

BPlusTree/BPlusTree.Test/ThreadedBTreeTest.cs

C#Copy Code
BPlusTree<KeyInfo, DataValue>.OptionsV2 options = new BPlusTree<KeyInfo, DataValue>.OptionsV2(
    new KeyInfoSerializer(), new DataValueSerializer(), new KeyInfoComparer());
            
const int keysize = 16 + 4;
const int valuesize = keysize + 256 + 44;

options.CalcBTreeOrder(keysize, valuesize); 
options.FileName = TempFile.TempPath;
options.CreateFile = CreatePolicy.Always;
options.FileBlockSize = 8192;
options.StorageType = StorageType.Disk;
            
options.CacheKeepAliveTimeout = 10000;
options.CacheKeepAliveMinimumHistory = 0;
options.CacheKeepAliveMaximumHistory = 200;
            
options.CallLevelLock = new ReaderWriterLocking();
options.LockingFactory = new LockFactory<SimpleReadWriteLocking>();
options.LockTimeout = 10000;

using(BPlusTree<KeyInfo, DataValue> dictionary = new BPlusTree<KeyInfo, DataValue>(options))
using(WorkQueue work = new WorkQueue(Environment.ProcessorCount))
{
    Exception lastError = null;
    work.OnError += delegate(object o, ErrorEventArgs e) { lastError = e.GetException(); };

    for (int i = 0; i < Environment.ProcessorCount; i++)
        work.Enqueue(new ThreadedTest(dictionary, 1000).Run);

    Assert.IsTrue(work.Complete(true, 60000));
    Assert.IsNull(lastError, "Exception raised in worker: {0}", lastError);
}
VB.NETCopy Code
Dim options As New BPlusTree(Of KeyInfo, DataValue).OptionsV2(New KeyInfoSerializer(), New DataValueSerializer(), New KeyInfoComparer())

Const  keysize As Integer = 16 + 4
Const  valuesize As Integer = keysize + 256 + 44

options.CalcBTreeOrder(keysize, valuesize)
options.FileName = TempFile.TempPath
options.CreateFile = CreatePolicy.Always
options.FileBlockSize = 8192
options.StorageType = StorageType.Disk

options.CacheKeepAliveTimeout = 10000
options.CacheKeepAliveMinimumHistory = 0
options.CacheKeepAliveMaximumHistory = 200

options.CallLevelLock = New ReaderWriterLocking()
options.LockingFactory = New LockFactory(Of SimpleReadWriteLocking)()
options.LockTimeout = 10000

Using dictionary As New BPlusTree(Of KeyInfo, DataValue)(options)
    Using work As New WorkQueue(Environment.ProcessorCount)
        Dim lastError As Exception = Nothing
        work.OnError += Function(o As Object, e As ErrorEventArgs) Do
            lastError = e.GetException()
        End Function

        Dim i As Integer = 0
        While i < Environment.ProcessorCount
            work.Enqueue(New ThreadedTest(dictionary, 1000).Run)
            System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
        End While

        Assert.IsTrue(work.Complete(True, 60000))
        Assert.IsNull(lastError, "Exception raised in worker: {0}", lastError)
    End Using
End Using

Requirements

Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7

See Also

Generated with Document! X 2011 by Innovasys